/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scm.struts.actions;

import com.scm.hibernate.entities.Admin;
import com.scm.hibernate.entities.Branch;
import com.scm.hibernate.entities.Division;
import com.scm.hibernate.entities.Exam;
import com.scm.hibernate.entities.Faculty;
import com.scm.hibernate.entities.Lecture;
import com.scm.hibernate.entities.Question;
import com.scm.hibernate.entities.Student;
import com.scm.hibernate.entities.Subject;
import com.scm.hibernate.entities.Test;
import com.scm.struts.forms.UserLoginForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author unnati
 */
public class UserLoginAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static String path;

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //getting all the divisions
        List<Division> all_divisions = null;
        all_divisions = (List<Division>) request.getServletContext().getAttribute("alldivisions");

        System.err.println("yash ="+all_divisions);
        if (all_divisions == null) {
            all_divisions = (List<Division>) new Division().getAll();
            for (Division d : all_divisions) {
                d.setBr(d.getBid());
            }
            request.getServletContext().setAttribute("alldivisions", all_divisions);
        }

        // getting all the branches 
        List<Branch> all_branches = null;
        all_branches = (List<Branch>) request.getServletContext().getAttribute("allbranches");
        // all_branches = (List<Branch>) request.getSession().getAttribute("allbranches");
        if (all_branches == null) {
            all_branches = (List<Branch>) new Branch().getAll();
            request.getServletContext().setAttribute("allbranches", all_branches);
        }

        // getting all the lectures 
        List<Lecture> all_lectures = null;
        all_lectures = (List<Lecture>) request.getServletContext().getAttribute("alllectures");
        if (all_lectures == null) {
            all_lectures = (List<Lecture>) new Lecture().getAll();
            request.getServletContext().setAttribute("alllectures", all_lectures);
        }

        List<Subject> all_subjects = null;
        all_subjects = (List<Subject>) request.getServletContext().getAttribute("allsubjects");
        if (all_subjects == null) {
            all_subjects = (List<Subject>) new Subject().getAll();
            request.getServletContext().setAttribute("allsubjects", all_subjects);
        }

        // getting all the branches 
        List<Faculty> all_faculties = null;
        all_faculties = (List<Faculty>) request.getServletContext().getAttribute("allfaculties");
        // all_branches = (List<Branch>) request.getSession().getAttribute("allbranches");
        if (all_faculties == null) {
            all_faculties = (List<Faculty>) new Faculty().getAll();
            request.getServletContext().setAttribute("allfaculties", all_faculties);
        }

        UserLoginForm ulf = (UserLoginForm) form;
        // System.out.println(ulf.getUsertype());

        if ("student".equals(ulf.getUsertype())) {
            Student student = new Student();
            // System.out.println(ulf.getEmail());
            student.setEmail(ulf.getEmail());
            student.setPassword(ulf.getPassword());

            student = student.verify();
            if (student != null) {
                path = "user_home";
                request.getSession().setAttribute("student", student);
                request.getSession().setAttribute("usertype", ulf.getUsertype());
            } else {
                path = "user_login";
            }
        }

        if ("faculty".equals(ulf.getUsertype())) {
            Faculty f = new Faculty();

            f.setEmail(ulf.getEmail());
            f.setPassword(ulf.getPassword());

            f = f.verify();
            if (f != null) {

//                List<Student> li = Student.getallstudents();
//                //  List<Faculty> fac_li = Faculty.getallfaculties();
//                double iteration = Math.ceil((float) li.size() / 5);
//                request.getSession().setAttribute("iteration", iteration);

                Exam e=new Exam();
                List<Object> li_exam=(List<Object>)(Object)e.getAll();
//                double iteration_exam=Math.ceil((float)li_exam.size()/5);
//                request.getSession().setAttribute("iteration_exam", iteration_exam);
//                ArrayList<Object> filter_exam=Student.page(li_exam);
//                
//                Test t=new Test();
//                List<Object> li_test=(List<Object>)(Object)t.getAll();
//                double iteration_test=Math.ceil((float)li_test.size()/5);
//                request.getSession().setAttribute("iteration_test", iteration_test);
//                ArrayList<Object> filter_test=Student.page(li_test);
//                
//                Question q=new Question();
//                List<Object> li_question=(List<Object>)(Object)q.getAll();
//                double iteration_question=Math.ceil((float)li_question.size()/5);
//                request.getSession().setAttribute("iteration_question", iteration_question);
//                ArrayList<Object> filter_question=Student.page(li_question);
//
//
//                ArrayList<Student> filtered_list = new ArrayList<>();
//                int end = 5;
//                if (li.size() < 5) {
//                    end = li.size();
//                }
//                for (int i = 0; i < end; i++) {
//                    filtered_list.add(li.get(i));
//                }
                request.getSession().setAttribute("user", f);
                request.getSession().setAttribute("usertype", "faculty");
                request.getSession().setAttribute("exam_viewlist", li_exam);
                
//                request.getSession().setAttribute("student_viewlist", filtered_list);
                //  request.getSession().setAttribute("faculty_viewlist", fac_li);
                
                path = "user_home";
            } else {
                path = "user_login";
            }

        }

        if ("admin".equals(ulf.getUsertype())) {
            Admin ad = new Admin();

            ad.setEmail(ulf.getEmail());
            ad.setPassword(ulf.getPassword());

            ad = ad.verify();
            if (ad != null) {
                List<Student> li = Student.getallstudents();

                double iteration = Math.ceil((float) li.size() / 5);
                request.getSession().setAttribute("iteration", iteration);

                ArrayList<Student> filtered_list = new ArrayList<>();
                int end = 5;
                if (li.size() < 5) {
                    end = li.size();
                }
                for (int i = 0; i < end; i++) {
                    System.out.println(li.get(i));
                    filtered_list.add(li.get(i));
                }

                request.getSession().setAttribute("student_viewlist", filtered_list);
                List<Faculty> fac_li = (List<Faculty>) new Faculty().getAll();
                request.getSession().setAttribute("faculty_viewlist", fac_li);

                path = "user_home";
                System.out.println("Home = " + path);
                request.getSession().setAttribute("admin", ad);
                request.getSession().setAttribute("usertype", ulf.getUsertype());
            } else {
                path = "user_login";
            }
        }

        return mapping.findForward(path);
    }
}
